<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>操作员管理</el-breadcrumb-item>
    </el-breadcrumb>

    <br />
    <el-col :span="24">
      <el-col :span="8">
        <el-input v-model="hrname" placeholder="通过用户名搜索"></el-input>
      </el-col>

      <el-col :span="4">
        <el-button :span="4" type="primary" @click="selectHr()">搜索</el-button>
      </el-col>
    </el-col>

    <el-card
      class="box-card"
      v-for="(hr, index) in Hrs"
      :key="index"
      style="display: inline-block; margin-left: 3px"
    >
      <div slot="header" class="clearfix">
        <span>{{ hr.name }}</span>
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
          @click="deleteHr(hr.id)"
          ><i class="el-icon-delete" style="color: red"></i
        ></el-button>
      </div>
      <div class="text item">
        <p>用户名：{{ hr.name }}</p>
        <p>手机号码：{{ hr.phone }}</p>
        <p>电话号码：{{ hr.telephone }}</p>
        <p>地址：{{ hr.address }}</p>
        <p>
          用户状态：<el-switch
            v-model="Hrs[index].enabled"
            active-text="启用"
            inactive-text="禁用"
          >
          </el-switch>
        </p>
        <p>
          用户角色：<span v-for="role in hr.roles" :key="role.id"
            >{{ role.nameZh }}
          </span>
          <el-popover placement="right" width="350" trigger="click">
            <el-select v-model="value" multiple placeholder="请选择">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.nameZh"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-button
              type="primary"
              style="margin-left: 10px"
              @click="updateHrRole(hr.id)"
              >确定</el-button
            >
            <i
              class="el-icon-more"
              style="color: blue"
              slot="reference"
              @click="morenrole(hr.roles)"
              @after-leave="updateHrRole()"
            ></i>
          </el-popover>
        </p>
        <p>备注:</p>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "SysHr",
  data() {
    return {
      Hrs: [],
      hrname: "",
      value: [],
      roles: [],
    };
  },
  created() {
    this.getAllHr();
    this.getAllRole();
  },
  methods: {
    getAllHr() {
      this.$axios
        .post("HrController/getAllHrAndRole", this.$qs.stringify({}))
        .then((value) => {
          this.Hrs = value.data;
          for (let i = 0; i < value.data.length; i++) {
            if (value.data[i].enabled == "1") {
              this.Hrs[i].enabled = true;
            } else {
              this.Hrs[i].enabled = false;
            }
          }
        })
        .catch((value) => {
          console.error(value);
        });
    },
    getHrByName() {
      this.$axios
        .post(
          "HrController/getHrAndRoleByName",
          this.$qs.stringify({ name: this.hrname })
        )
        .then((value) => {
          this.Hrs = value.data;
          for (let i = 0; i < value.data.length; i++) {
            if (value.data[i].enabled == "1") {
              this.Hrs[i].enabled = true;
            } else {
              this.Hrs[i].enabled = false;
            }
          }
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 模糊查询
    selectHr() {
      if (this.hrname == "") {
        this.getAllHr();
      } else {
        this.getHrByName();
      }
    },
    getAllRole() {
      this.$axios
        .post("RoleController/getAllRole", this.$qs.stringify({}))
        .then((value) => {
          this.roles = value.data;
        })
        .catch((value) => {
          console.error(value);
        });
    },
    morenrole(roles) {
      this.value = [];
      for (let i = 0; i < roles.length; i++) {
        this.value.push(roles[i].id);
      }
    },
    // 修改role
    updateHrRole(hrId) {
      this.$axios
        .post(
          "HrController/updateHrRole",
          this.$qs.stringify({
            hrId: hrId,
            roleIds: this.value,
          })
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "更新成功",
              type: "success",
            });
          }
          this.getAllHr();
        })
        .catch((value) => {
          console.error(value);
        });
    },
    // 删除
    deleteHr(hrId) {
      this.$axios
        .post(
          "HrController/deleteHrById",
          this.$qs.stringify({
            id: hrId,
          })
        )
        .then((value) => {
          if (value.data > 0) {
            this.$message({
              message: "删除成功",
              type: "success",
            });
          }
          this.getAllHr();
        })
        .catch((value) => {
          if (value != null) {
            this.$message({
              message: "该数据含有关联数据，删除失败",
              type: "error",
            });
          }
        });
    },
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 350px;
  height: 350px;
}
</style>